package cn.study.hashMapTest;

import java.util.HashMap;
import java.util.Map;
/**
 * @Author     ：跟大家一起学架构
 * @Description：迭代器remove，可以避免并发修改异常
 */
public class HashMapRemove {

    public static void main(String[] args) {
        final Map<String, String> map = new HashMap<String, String>();
        map.put("A","A");
        map.put("B","B");
        new Thread() {
            @Override
            public void run() {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    System.out.println(entry.getKey()+entry.getValue());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
//                map.remove("B");
                map.entrySet().remove("B");
            }
        }.start();

    }
}
